<?php
namespace App\Lib\Redis;

use EasySwoole\Config;
use EasySwoole\Core\AbstractInterface\Singleton;

class Redis
{
    use Singleton;

    public $redis = null;

    private function __construct()
    {
        //判断redis扩展是否安装
        if(!extension_loaded('redis')){
            throw new \Exception('no redis extension');
        }

        //连接redis
        try {
            //1.通过配置文件获取redis配置
            //$redisConf = Config::getInstance()->getConf('redis');

            //使用yaconf获取Redis配置信息, 性能更好
            $redisConf = \Yaconf::get('redis');

            //2.连接redis
            $this->redis = new \Redis();
            $con = $this->redis->connect($redisConf['host'], $redisConf['port'], $redisConf['timeout']);
            $this->redis->auth($redisConf['password']);
        }catch (\Exception $e){
            echo $e->getMessage();
            throw new \Exception('redis server error');
        }

        if ($con === false){
            throw new \Exception('redis conn error');
        }
    }

    public function get($key){
        return $this->redis->get($key);
    }

    public function lPop($key){
        if(empty($key)){
            return '';
        }
        return $this->redis->lPop($key);
    }

    public function rPush($key, $value){
        if(empty($key)) {
            return '';
        }
        return $this->redis->rPush($key, $value);
    }


}